package com.bungarus.busi;

import com.bungarus.Access;
import com.bungarus.event.AuthSucceededEvent;
import com.bungarus.event.SessionCreatedEvent;
import com.bungarus.model.Member;
import com.bungarus.model.MessageBuilder;
import com.bungarus.model.Session;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import org.apache.log4j.Logger;

/**
 *
 * Created by tang on 2018/11/11.
 */
public class CreateSessionHandler extends ChannelInboundHandlerAdapter {
    Logger logger = Logger.getLogger(CreateSessionHandler.class);
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof AuthSucceededEvent){
            AuthSucceededEvent event = (AuthSucceededEvent)evt;
            Member member = event.getMember();
            logger.debug("================ userEventTriggered: member is " + member.toString());
            if(!event.isReAuth()){ // logic of auth
                Session session = Session.newBuilder()
                        .id(ctx.channel().id())
                        .token(null)//use default token generator
                        .user(String.valueOf(member.getId()))
                        .app(event.getHeader().getApp())
                        .device(event.getHeader().getDev())
                        .tenant(member.getTenantId())
                        .ip(((SocketChannel) ctx.channel()).localAddress().getHostString())
                        .port(Access.PORT_7800)
                        .build();
                //save the new session into memory
                Access.getSessionContainer().add(session, ctx.channel());
                //send the session info to nodes in transfer layer
                SessionCreatedEvent sessionCreatedEvent = new SessionCreatedEvent(session);
                ctx.fireUserEventTriggered(sessionCreatedEvent);
            }
            else { // logic of re-auth

            }
        }
        else {
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if(cause instanceof AuthenticationInputException){
             ctx.writeAndFlush(MessageBuilder.buildAuthErrorMessage());
        }
        else if(cause instanceof BusiTimeoutException){
            ctx.writeAndFlush(MessageBuilder.buildTimeoutMessage());
        }
        else if(cause instanceof UserNonexistException){
            ctx.writeAndFlush(MessageBuilder.buildUserNonexistMessage());
        }
        else if(cause instanceof AuthenticationExpirationException){
            ctx.writeAndFlush(MessageBuilder.buildAuthExpirationMessage());
        }
    }
}
